package com.jiabowen.store.service.impl;

import com.jiabowen.store.entity.Cart;
import com.jiabowen.store.entity.Product;
import com.jiabowen.store.mapper.CartMapper;
import com.jiabowen.store.mapper.ProductMapper;
import com.jiabowen.store.service.ICartService;
import com.jiabowen.store.service.IProductService;
import com.jiabowen.store.service.exception.*;
import com.jiabowen.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private IProductService productService;


    @Override
    public void addToCart(Integer pid, Integer uid, Integer amount, String username) {

        //查询当前要添加的这个购物车在表中是否已存在
        Cart result = cartMapper.findByUidAndPid(pid, uid);

        Date date = new Date();

        if (result == null){//表示这个商品从来都没被加入过购物车
            Product product = productService.findProductById(pid);
            Long price = product.getPrice();
            //创建一个cart对象
            Cart cart = new Cart();
            //补全数据
            cart.setPrice(price);
            cart.setNum(amount);
            cart.setUid(uid);
            cart.setPid(pid);

            cart.setModifiedUser(username);
            cart.setModifiedTime(date);
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);

            //执行插入操作
            Integer rows = cartMapper.insert(cart);
            if (rows != 1){
                throw new InsertException("插入数据时产生未知的异常");
            }
        } else {//表示当前商品已经存在于购物车中，进行修改操作
            Integer num = result.getNum();
            num = num + amount;

            Integer rows = cartMapper.updateNumByCid(result.getCid(), num, username, date);
            if (rows != 1){
                throw new UpdateException("更新数据时产生未知异常");
            }
        }
    }

    @Override
    public List<CartVO> findVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("数据不存在异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问异常");
        }
        Integer num = result.getNum() + 1;
        Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新数据失败");
        }
        //返回新的购物车数据总量
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("数据不存在异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问异常");
        }
        Integer num = result.getNum() - 1;
        Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新数据失败");
        }
        //返回新的购物车数据总量
        return num;
    }

    @Override
    public List<CartVO> findVOByCid(Integer[] cids,Integer uid) {
        List<CartVO> list = cartMapper.findVOByCid(cids);
//        for (CartVO cartVO : list) {
//            if (!cartVO.getUid().equals(uid)){
//                list.remove(cartVO);
//            }
//        }

        Iterator<CartVO> it = list.iterator();
        while (it.hasNext()){
            CartVO cartVO = it.next();
            if (!cartVO.getUid().equals(uid)){//表示当前数据不属于当前用户
                //从集合中移除这个元素
                it.remove();
            }
        }
        return list;
    }
}
